package com.example.content_client;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.content_client.entity.User;
import com.example.content_client.util.ToastUtil;

public class ContentWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etClientName;
    private EditText etClientAge;
    private CheckBox ckClickMarried;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_write);

        etClientName = findViewById(R.id.et_clientName);
        etClientAge = findViewById(R.id.et_clientAge);
        ckClickMarried = findViewById(R.id.cK_clickMarry);

        findViewById(R.id.btn_clickSaveData).setOnClickListener(this);
        findViewById(R.id.btn_clickDeleteData).setOnClickListener(this);
        findViewById(R.id.btn_clickUpdateData).setOnClickListener(this);
        findViewById(R.id.btn_clickQueryData).setOnClickListener(this);

    }


    @SuppressLint("Range")
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_clickSaveData:
                ContentValues values = new ContentValues();
                values.put(UserInfoContent.USER_NAME,etClientName.getText().toString());
                values.put(UserInfoContent.USER_AGE,Integer.parseInt(etClientAge.getText().toString()));
                values.put(UserInfoContent.USER_MARRIED,ckClickMarried.isChecked());
                getContentResolver().insert(UserInfoContent.CONTENT_URL,values);
                ToastUtil.successShow(this,"保存成功");
                break;

            case R.id.btn_clickQueryData:
                Cursor cursor = getContentResolver().query(UserInfoContent.CONTENT_URL, null, null, null, null);
                if (cursor != null){
                    while(cursor.moveToNext()){
                        User user = new User();
                        user.id = cursor.getInt(cursor.getColumnIndex(UserInfoContent._ID));
                        user.name = cursor.getString(cursor.getColumnIndex(UserInfoContent.USER_NAME));
                        user.age = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_AGE));
                        user.married = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_MARRIED))==1 ? true : false;
                        Log.d("ning",user.toString());
                    }
                }
                cursor.close();
                break;

            case R.id.btn_clickDeleteData:
                break;
        }

    }
}